package com.qianbei.view.util;

import android.text.TextUtils;
import android.util.Log;

import com.qianbei.bean.GetBindBean2;
import com.qianbei.bean.GetBox;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.common.net.okhttp.RequeastBean;
import com.qianbei.common.net.view.MsgAlert;
import com.qianbei.view.EpcDataBase;
import com.qianbei.view.db.Box;
import com.qianbei.view.orm.RFID;
import com.qianbei.view.util.MessageCallback;
import com.qianbei.view.db.BoxDb;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bliu on 2018/3/16.
 */

public class JobBoxUtil {
    private final static String[] boxTypeStr = {"医废小箱", "医废大箱", "医塑小箱", "医塑大箱"};

    /**
     * 按RFID 查询周转箱信息
     * @param boxDb
     * @param epcDataBase
     * @param callback
     */
    public static void getBoxType(final BoxDb boxDb, final EpcDataBase epcDataBase, final MessageCallback callback) {
        Log.d("getBoxInfo", "1");

        if(TextUtils.isEmpty(epcDataBase.epc)){
            getBoxTypeByNumber(boxDb,epcDataBase,callback);
            return;
        }

        ArrayList<String> rfid = new ArrayList<>();
        rfid.add(epcDataBase.epc);
        final Box box = boxDb.getBox(epcDataBase.epc,null);
        if (box != null) {
            epcDataBase.typeValue = String.valueOf(box.getBoxType());
            epcDataBase.boxType = boxTypeStr[box.getBoxType() - 1];
            epcDataBase.boxId = box.getBoxNum();
            epcDataBase.epc = box.getRfid();

        }
        callback.call();
        RequeastBean bean = new RequeastBean("boxInfo",
                "com.cygps.dubbo.WasteTransport.IPdaBoxManagement",
                new GetBindBean2(rfid));
        ServerControl sc = new ServerControl(bean);
        sc.serverListener = new ServerControl.ServerListener() {

            @Override
            public void serverFinish(ServerResult sr) {
                if (sr.isContinue) {
                    JSONObject rfidRes = sr.bodyData.optJSONArray("data").optJSONObject(0);
                    if (rfidRes.optInt("code") != -1) {
                        epcDataBase.boxType = rfidRes.optString("boxTypeVal");
                        epcDataBase.boxId = rfidRes.optString("boxNo");
                        epcDataBase.typeValue = rfidRes.optString("boxType");


                        //如果本地库中没有当前周转箱 或信息不一致 更新本地库
                        if (box == null || !box.getBoxNum().equals(epcDataBase.boxId) || !String.valueOf(box.getBoxType()).equals(epcDataBase.typeValue)) {
                            updateLocalBox(boxDb, epcDataBase);
                        }

                    }else{
                        epcDataBase.boxType=null;
                        epcDataBase.boxId=null;
                        epcDataBase.typeValue = null;

                    }

                    callback.call();
                }
            }
        };
        sc.OKpost();
    }

    /**
     * 按周转箱编号查  此方法供 getBoxType 使用 完杨箱号或RFID方式查询周转箱功能
     * @param boxDb
     * @param epcDataBase
     * @param callback
     */
    private static void getBoxTypeByNumber(final BoxDb boxDb, final EpcDataBase epcDataBase, final MessageCallback callback) {
        Log.d("getBoxTypeByNumber", "1");


        final Box box = boxDb.getBox(null, epcDataBase.boxId);
        if (box != null) {
            epcDataBase.typeValue = String.valueOf(box.getBoxType());
            epcDataBase.boxType = boxTypeStr[box.getBoxType() - 1];
            epcDataBase.epc = box.getRfid();


        }

        callback.call();
        if (TextUtils.isEmpty(epcDataBase.getBoxId())) {
            new MsgAlert().show("缺少箱号信息！！！");
            return;
        }
        RequeastBean bean = new RequeastBean("boxInfoByNum",
                "com.cygps.dubbo.WasteTransport.IPdaBoxManagement",
                new GetBox(epcDataBase.getBoxId().trim()));
        ServerControl sc = new ServerControl(bean);
        sc.serverListener = new ServerControl.ServerListener() {

            @Override
            public void serverFinish(ServerResult sr) {
                if (sr.isContinue) {
                    JSONObject rfidRes = sr.bodyData.optJSONArray("data").optJSONObject(0);
                    if (rfidRes.optInt("code") != -1) {
                        epcDataBase.epc = rfidRes.optString("rfid");
                        epcDataBase.boxType = rfidRes.optString("boxTypeVal");
                        epcDataBase.typeValue = rfidRes.optString("boxType");


                        //如果本地库中没有当前周转箱 或信息不一致 更新本地库
                        if (box == null || !box.getBoxNum().equals(epcDataBase.boxId) || !String.valueOf(box.getBoxType()).equals(epcDataBase.typeValue)) {
                            updateLocalBox(boxDb, epcDataBase);
                        }
                    }else{
                        epcDataBase.epc = null;
                        epcDataBase.boxType=null;
                        epcDataBase.typeValue = null;

                    }

                    callback.call();

                }
            }
        };
        sc.OKpost();
    }

    public static void updateLocalBox(BoxDb boxDb, EpcDataBase item) {
        Box updateBox = new Box();
        if (TextUtils.isEmpty(item.typeValue)) {
            return;
        }
        updateBox.setBoxType(Integer.valueOf(item.typeValue));
        updateBox.setRfid(item.epc);
        updateBox.setBoxNum(item.boxId);

        boxDb.update(updateBox);
    }

    public String countBox(List<RFID> box, boolean shortTxt){
        BoxCount count = new BoxCount(box, shortTxt);
        String txt = count.countBox();

        return txt;
    }

     class BoxCount {
        private int big = 0;
        private int small = 0;
        private int boxUpload = 0;
        private List<RFID> rfid;
        private boolean shortTxt = false;

        public BoxCount(List<RFID> box) {
            this.rfid = box;
        }

        public BoxCount(List<RFID> box, boolean shortTxt) {
            this.rfid = box;
            this.shortTxt = shortTxt;
        }

        public int getBig() {
            return big;
        }

        public int getSmall() {
            return small;
        }

        public int getBoxUpload() {
            return boxUpload;
        }

        public String countBox() {
            if (this.rfid == null) {
                return "";
            }

            for (RFID one : rfid) {
                if (one.getBoxTypeIntVal() != 0) {
                    int val = one.getBoxTypeIntVal();
                    if (val == 1 || val == 3) {
                        small++;
                    } else if (val == 2 || val == 4) {
                        big++;
                    }
                }
            }

            boxUpload = rfid.size()-big-small;

            String countTxt = countTxt();
            return countTxt;

        }

        private String countTxt() {
            String boxCount = "大箱: " + big + "  小箱: " + small;
            if (shortTxt) {
                boxCount = "大: " + big + "  小: " + small;
            }

            if (boxUpload > 0) {
                boxCount += "  箱号装车: " + boxUpload;
            }

            //没网情况下得不到大小箱数
            if (small == 0 && big == 0 && boxUpload == 0) {
                boxCount = "大箱: 0  小箱: 0";
            }

            return boxCount;
        }
    }
}
